package cn.itsource.basic.controller;

import cn.itsource.basic.dto.PhoneRegisterDto;
import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.IVerifyCodeService;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /*
    * 获取图形验证码
    * */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult image(@PathVariable String imageCodeKey){
        try {
            String base64Str = verifyCodeService.image(imageCodeKey);
            return AjaxResult.me().setResultObj(base64Str);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取验证码图片失败！"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取验证码图片失败！"+e.getMessage());
        }
    }

    /**
     * 获取手机验证码
     * @param phoneRegisterDto
     * @return
     */
    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody PhoneRegisterDto phoneRegisterDto){
        try {
            verifyCodeService.sendSmsCode(phoneRegisterDto);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送手机验证码失败！"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送手机验证码失败！"+e.getMessage());
        }
    }
}
